// Calculate the magnitude of a vector by summing the squares of its coordinates
// and taking the square root. Use the `sqrt()` method to calculate the square
// root, like `v.sqrt()`.


fn magnitude(points: &[f64; 3]) -> f64 {
    points
        .iter()
        .map(|v| v * v)
        .sum::<f64>()
        .sqrt()
}

// Normalize a vector by calculating its magnitude and dividing all of its
// coordinates by that magnitude.


fn normalize(points: &mut [f64; 3]) {
    let magnitude = magnitude(points);
    for i in 0..points.len() {
        points[i] /= magnitude;
    }
}

// Use the following `main` to test your work.

#[test]
fn test() {
    println!("Magnitude of a unit vector: {}", magnitude(&[0.0, 1.0, 0.0]));

    let mut v = [1.0, 2.0, 9.0];
    println!("Magnitude of {v:?}: {}", magnitude(&v));
    normalize(&mut v);
    println!("Magnitude of {v:?} after normalization: {}", magnitude(&v));
}